const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter

Page({

  /**
   * 页面的初始数据
   */
  data: {
    search_word:'',
    no_more:false,
    label_index:0,
    windowWidth:wx.getSystemInfoSync().windowWidth,
    article_list:[],
    file_type_list:[
      {file_type:'',file_type_name:'全部'},
      {file_type:2,file_type_name:'法规'},
      {file_type:1,file_type_name:'文章'},
      {file_type:3,file_type_name:'说明书'}
    ],
  },

  
  // 设置文件类型
  set_label(e){
    getApp().get_vibration()
    let index = e.currentTarget.id
    let file_type = this.data.file_type_list[index].file_type
    this.setData({
      label_index:index,
      search_word:'',
      no_more:false,
    })
    this.file_type = file_type
    this.page_no = 0
    this.load_article_list('', this.page_no, this.file_type)
  },


  set_file_type(e){
    getApp().get_vibration()
    let index = e.detail.value
    let file_type = this.data.file_type_list[index].file_type
    let file_type_name = this.data.file_type_list[index].file_type_name
    this.setData({
      file_type_name:file_type_name,
      search_word:'',
      no_more:false,
    })
    this.file_type = file_type
    this.page_no = 0
    this.load_article_list('', this.page_no, this.file_type)
  },

  // 取消搜索
  cancel(){
    getApp().get_vibration()
    this.onLoad(this.options)
  },

  // 输入关键字
  input_search(e){
    let search_word = e.detail.value
    this.setData({
      search_word:search_word,
    })
  },

  go_search(){
    getApp().get_vibration()
    this.setData({
      no_more:false,
      label_index:0,
    })
    this.file_type=''
    this.page_no = 0
    this.load_article_list(this.data.search_word,this.page_no,this.file_type)
  },

  // 编辑视频
  edit_video(e){
    let index = e.currentTarget.id
    let video_id = this.data.article_list[index].video_id
    let itemList = ['编辑','删除']
    wx.showActionSheet({
      itemList: itemList,
      success: res => {
        let sel = itemList[res.tapIndex]
        if(sel=='编辑'){
          wx.navigateTo({
            url: '/pages/edit_video/edit_video?video_type=gs&video_id=' + video_id,
          })
        }else if(sel=='删除'){
          this.delete_video('gs',video_id)
        }
      },
    })
  },

  // 删除视频
  delete_video(video_type,video_id){
    wx.showModal({
      content: '确定删除该视频吗？',
      showCancel: true,
      title: '提示',
      success: res => {
        if(res.confirm){

          wx.request({
            url: domainName + '/delete_video',
            data: {
              video_type:video_type,
              video_id:video_id,
            },...parameter,
            success: res => {
              if(res.data.delete_res){
                wx.showToast({
                  title: '删除成功',
                  duration: 1000,
                  icon: 'success',
                })
                setTimeout(() => {
                  this.onShow()
                }, 500);
              }
            },
          })
        }
      },
    })
  },



  // 选择视频
  tap_article(e){
    // let has_right = this.data.has_right
    // if(has_right){
      let index = e.currentTarget.id
      let article_title = this.data.article_list[index].article_title
      let article_url = this.data.article_list[index].article_url
      getApp().get_vibration()
      wx.navigateTo({
        url: '/pages/information/text_details/text_details?article_url=' + article_url + '&article_title=' + article_title,
      })
  },

  // 加载模拟飞行视频列表
  load_article_list(search_word,page_no,file_type){
    wx.showLoading({
      title: '网络通讯中',
      mask: false,
    })
    wx.request({
      url: domainName + '/load_article_list',
      data: {
        search_word:search_word,
        page_no:page_no,
        file_type:file_type,
      },...parameter,
      success: res => {
        wx.hideLoading()
        let article_list = res.data.article_list
        if(page_no>0){
          this.setData({
            article_list:this.data.article_list.concat(article_list),
          })
        }else{
          this.setData({
            article_list:article_list,
          })
        }
        this.page_no += 1
        if(article_list.length==0){
          this.setData({
            no_more:true
          })
        }
        
      },
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.file_type = ''
    this.page_no = 0
    this.load_article_list('',this.page_no,this.file_type)
  },

  onReady(){
    this.setData({
      windowHeight:wx.getSystemInfoSync().windowHeight,
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.no_more){
      this.load_article_list(this.data.search_word, this.page_no, this.file_type)
    }
  },

})